/**
 * 返回非负数字x中从右开始p位置的n个比特取反
 */

#include <stdio.h>

unsigned invertbits(unsigned x, int p, int n);

int main() {
    unsigned x = 255;
    int p = 7;
    int n = 2;
    printf("0x%x\n", invertbits(x, p, n));

    return 0;
}

unsigned invertbits(unsigned x, int p, int n) {
    return x ^ (((1 << n) - 1) << (p - n + 1));
}
